package cn.wjx.taobaounion.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.wjx.taobaounion.R;
import cn.wjx.taobaounion.model.domain.IBaseGoodsItemInfo;
import cn.wjx.taobaounion.utils.URLUtils;

/**
 * @author WuChangJian
 * @date 2020/6/19 11:12
 */
public class HomeContentListAdapter extends RecyclerView.Adapter<HomeContentListAdapter.InnerHolder> {

    private List<IBaseGoodsItemInfo> mContentList = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_content_list, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        holder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.OnItemClick(mContentList.get(position));
            }
        });
        holder.setData(position);
    }

    @Override
    public int getItemCount() {
        return mContentList.size();
    }

    public void setData(List<? extends IBaseGoodsItemInfo> contents) {
        mContentList.clear();
        mContentList.addAll(contents);
        notifyDataSetChanged();
    }

    public void addData(List<? extends IBaseGoodsItemInfo> contents) {
        int oldSize = mContentList.size();
        mContentList.addAll(contents);
        // 局部更新
        notifyItemRangeChanged(oldSize, contents.size());
    }

    public interface OnItemClickListener {
        void OnItemClick(IBaseGoodsItemInfo item);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.goods_title)
        public TextView goodsTitle;

        @BindView(R.id.goods_cover)
        public ImageView goodsCover;

        @BindView(R.id.off_prise)
        public TextView offPriseTv;

        @BindView(R.id.original_price)
        public TextView originalPriceTv;

        @BindView(R.id.after_coupon_price)
        public TextView currentPriceTv;

        @BindView(R.id.goods_sell_volume)
        public TextView sellVolume;

        private InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        private void setData(int position) {
            IBaseGoodsItemInfo data = mContentList.get(position);
            Context context = itemView.getContext();
            float originPrice = Float.parseFloat(data.getFinalPrice());
            float currentPrice = Float.parseFloat(data.getFinalPrice()) - data.getCouponAmount();
            ViewGroup.LayoutParams layoutParams = goodsCover.getLayoutParams();
            int width = layoutParams.width;
            int height = layoutParams.height;
            // 修改默认图片尺寸，优化图片的加载时间 取到百整数在缩小
            int coverSize = ((width > height ? width : height) / 100 * 100) / 2 + 100;
            //LogUtils.d(this, "width-->" + width + " height-->" + height + " coverSize-->" + coverSize);
            goodsTitle.setText(data.getTitle());
            originalPriceTv.setText(String.format("%.2f", originPrice));
            originalPriceTv.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);// 设置中划线效果
            offPriseTv.setText(String.format(context.getString(R.string.text_goods_off_price), data.getCouponAmount()));
            currentPriceTv.setText(String.format("%.2f", currentPrice));
            sellVolume.setText(String.format(context.getString(R.string.text_goods_sell_volume), data.getVolume()));
            String coverPath = URLUtils.getCoverPath(data.getCover(), coverSize);
            //LogUtils.d(this, "pic url -- > " + coverPath);
            Glide.with(context).load(coverPath).into(goodsCover);
        }
    }
}
